package com.chongxinlaiguo.mapstruct.dto.convertor;

import com.alibaba.fastjson.JSON;
import com.chongxinlaiguo.mapstruct.dto.resp.remote.PayRemoteRespDTO;
import com.chongxinlaiguo.mapstruct.dto.resp.PayRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Objects;

/**
 * @Desc:
 * @Author: dengyanliang
 * @Date: 2025/5/24 14:56
 */
@Mapper
public interface RespConvertor {

    RespConvertor INSTANCE = Mappers.getMapper(RespConvertor.class);



    @Mapping(source = "extendInfo",target = "extendInfo") // 这一行要不要都行
    // 将对象转化为string字符串
    @Mapping(target = "extendInfo.payRequest",qualifiedByName = "parseObjectToStr")
    PayRespDTO convertor(PayRemoteRespDTO resp);


    /**
     * 自定义方法，为了将对象转化为string字符串
     * @param obj
     * @return
     */
    @Named("parseObjectToStr")
    default String parseObjectToStr(Object obj){
        if(Objects.isNull(obj)){
            return null;
        }
        return JSON.toJSONString(obj);
    }

}
